package org.jeecg.modules.pubmgr.service.impl;

import org.jeecg.modules.pubmgr.entity.Parameter;
import org.jeecg.modules.pubmgr.entity.Paramcode;
import org.jeecg.modules.pubmgr.mapper.ParamcodeMapper;
import org.jeecg.modules.pubmgr.mapper.ParameterMapper;
import org.jeecg.modules.pubmgr.service.IParameterService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 参数分类管理
 * @Author: jeecg-boot
 * @Date:   2019-05-23
 * @Version: V1.0
 */
@Service
public class ParameterServiceImpl extends ServiceImpl<ParameterMapper, Parameter> implements IParameterService {

	@Autowired
	private ParameterMapper parameterMapper;
	@Autowired
	private ParamcodeMapper paramcodeMapper;
	
	@Override
	@Transactional
	public void saveMain(Parameter parameter, List<Paramcode> paramcodeList) {
		parameterMapper.insert(parameter);
		for(Paramcode entity:paramcodeList) {
			//外键设置
			entity.setFdParamid(parameter.getId());
			paramcodeMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void updateMain(Parameter parameter,List<Paramcode> paramcodeList) {
		parameterMapper.updateById(parameter);
		
		//1.先删除子表数据
		paramcodeMapper.deleteByMainId(parameter.getId());
		
		//2.子表数据重新插入
		for(Paramcode entity:paramcodeList) {
			//外键设置
			entity.setFdParamid(parameter.getId());
			paramcodeMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		paramcodeMapper.deleteByMainId(id);
		parameterMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			paramcodeMapper.deleteByMainId(id.toString());
			parameterMapper.deleteById(id);
		}
	}
	
}
